package com.hongyi.common.framework.domain;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 组合注解类
 *
 * @author DingHao
 */
public class CombinationAnnotationElement implements AnnotatedElement {

    private final AnnotatedElement[] elements;

    public CombinationAnnotationElement(AnnotatedElement... elements) {
        this.elements = elements;
    }

    @Override
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        for (AnnotatedElement element : elements) {
            T annotation = element.getAnnotation(annotationClass);
            if (annotation != null) {
                return annotation;
            }
        }
        return null;
    }

    @Override
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
        for (AnnotatedElement element : elements) {
            if (element.isAnnotationPresent(annotationClass)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Annotation[] getAnnotations() {
        List<Annotation> annotations = new ArrayList<>();
        for (AnnotatedElement element : elements) {
            annotations.addAll(Arrays.asList(element.getAnnotations()));
        }
        return annotations.toArray(new Annotation[0]);
    }

    @Override
    public Annotation[] getDeclaredAnnotations() {
        List<Annotation> annotations = new ArrayList<>();
        for (AnnotatedElement element : elements) {
            annotations.addAll(Arrays.asList(element.getDeclaredAnnotations()));
        }
        return annotations.toArray(new Annotation[0]);
    }
}
